#李--csv--cf批量解决代码--最终

import os
import csv
import pandas as pd
import shutil

def calculate_wind_capacity_factor(speed, height):
    speed = speed * ((height / 10) ** 0.143)
    if speed <= 3:
        return 0
    elif speed >= 14.5 and speed <= 25:
        return 1
    elif speed > 25:
        return 0
    elif speed >= 3 and speed <= 6:
        k = 1.78 * (speed ** 5) - 40.636 * (speed ** 4) + 365.88 * (speed ** 3) - 1604.4 * (
                    speed ** 2) + 3460 * speed - 2960.184
        k = k / 1500  # Capacity factor calculation
        return k
    elif speed > 6 and speed <= 9:
        k = (-2.93333) * (speed ** 5) + 107.7 * (speed ** 4) - 1573.27 * (speed ** 3) + 11460.158 * (
                    speed ** 2) - 41528.1 * speed + 59908.2
        k = k / 1500  # Capacity factor calculation
        return k
    else:
        k = 0.15247 * (speed ** 6) - 11.090005 * (speed ** 5) + 334.2735 * (speed ** 4) - 5339.027 * (
                    speed ** 3) + 47583.517 * (speed ** 2) - 223812 * speed + 433774.2
        k = k / 1500  # Capacity factor calculation
        return k


def process_files(input_folder, output_folder):
    j = 0
    for folder in os.listdir(input_folder):
        in_folder_path = os.path.join(input_folder, folder)
        out_folder_path = os.path.join(output_folder, f"cf_{folder}")
        os.makedirs(out_folder_path, exist_ok=True)

        for file in os.listdir(in_folder_path):
            print(f"{j} Processing file: {file}")
            file_path = os.path.join(in_folder_path, file)
            out_file_path = os.path.join(out_folder_path, file)

            data = []
            with open(file_path, 'r') as f:
                reader = csv.reader(f)
                data = list(reader)
                data[0].append("cf_80")
                data[0].append("cf_100")
                data[0].append("cf_150")

                for row in data[1:]:
                    wind_speed = float(row[1])  # Assuming wind speed is in the second column
                    cf_80 = calculate_wind_capacity_factor(wind_speed, 80)
                    cf_100 = calculate_wind_capacity_factor(wind_speed, 100)
                    cf_150 = calculate_wind_capacity_factor(wind_speed, 150)
                    row.append(cf_80)
                    row.append(cf_100)
                    row.append(cf_150)

            with open(out_file_path, 'w', newline='') as file:
                writer = csv.writer(file)
                writer.writerows(data)
        print(f"Processing folder {folder} completed")
        j += 1

        # Move processed folder to specified location
        #shutil.move(in_folder_path, os.path.join(r"F:\data\CMIP6\ccsv_history", folder))


if __name__ == "__main__":
    input_folder_path = r"F:\data\CMIP6\csv_test"  # Input folder path
    output_folder_path = r"F:\data\CMIP6\cff_test"  # Output folder path
    process_files(input_folder_path, output_folder_path)



# 开启D:\py-demo\csv_cf.py内的操作
#os.system("python D:\py-demo\\aver_test.py")